{extend name="adminblue/base" /}
{block name="resources"/}
<script src="ADMIN_JS/My97DatePicker/WdatePicker.js" type="text/javascript"></script>
<link href="ADMIN_CSS/goods/editgoods.css" rel="stylesheet" type="text/css">
<link href="__STATIC__/blue/css/goods/add_goods.css" rel="stylesheet" type="text/css">
<style  type="text/css">
	.help-inline{height: 30px;line-height: 30px;}
	input[type="radio"]{margin-top:6px;}
</style>
{/block}
{block name="main"}
<div class="space-10"></div>
<div class="set-style">
	<dl>
		<dt>活动名称：</dt>
		<dd><input type="text" id="discount_name" maxlength="10" class="input-common"><p class="error">请输入活动名称</p></dd>

	</dl>
	<dl>
		<dt>活动对象：</dt>

		<dd class="goods_type_select_c" style="margin-left: -50px">
			<span class="goods_type_item  selected" data-goods-type="1" style="height: 20px" >
				<p class="type_name">全部</p>
				<i class="selected-icon"></i>
				<input type="radio" name="goods_type_crowd" value="1"  >
			</span>
			<span class="goods_type_item " data-goods-type="2" style="height: 20px">
				<p class="type_name">企业内部</p>
				<i class="selected-icon"></i>
				<input type="radio" name="goods_type_crowd" value="2">
			</span>
			<span class="goods_type_item " data-goods-type="3" style="height: 20px">
				<p class="type_name">企业外部</p>
				<i class="selected-icon"></i>
				<input type="radio" name="goods_type_crowd" value="3"  >
			</span>

		</dd>
	</dl>
	<dl>
		<dt>活动有效期：</dt>
		<dd>
			<input class="input-medium input-common harf" type="text" id="start_time" style="width:250px;"onclick="WdatePicker({skin:'twoer',dateFmt:'yyyy-MM-dd HH:mm:ss'})">
			<span class="mlr5">-</span>
			<input class="input-medium input-common harf" size="15" type="text" id="end_time" style="width:250px;"onclick="WdatePicker({skin:'twoer',dateFmt:'yyyy-MM-dd HH:mm:ss'})">
			<p class="error">请输入活动有效期</p>
			<p class="error">活动结束时间不能小于当前时间</p>
			<p class="error">活动结束时间不能小于开始时间</p>
		</dd>
	</dl>
	<!-- 所有决定商品弹框内容的条件和存放商品id的隐藏域 -->
	<span id="goods-condition">
		<input type="hidden" name="type" value="2"/>
		<input type="hidden" name="stock" value="1"/>
		<input type="hidden" name="goods_type" value="1,2"/>
		<input type="hidden" name="is_have_sku" value="1"/>
		<input type="hidden" name="state" value="1"/>
		<input type="hidden" id="goods_id_array" value="">
		<input type="hidden" id="range_type" value="0">
		<input type="hidden" id="is_show_select" value="0">
		<input type="hidden" id="action" value="discount">
	</span>
	{include file="adminblue/Promotion/goodsSelectDialog" /}

	<dl>
		<dt></dt>
		<dd>
			<button class="btn-common btn-big" onclick="addDiscount();">保存</button>
			<button class="btn-common-cancle btn-big" onclick="window.location.href = window.history.go(-1);">返回</button>
		</dd>
	</dl>
</div>

<script>

    //折扣
    function discount(event){
        var discount = parseFloat($(event).val()).toFixed(2);
        if(discount <0.01){
            $(event).val(0.01);
            discount = 0.01;
        }else if(discount >10){
            $(event).val(10);
            discount = 10;
        }
        var goodsid = $(event).attr("goodsid");
        $(event).val(discount);
    }

    //保存
    var flag = false;//防止重复提交
    function addDiscount(){
        var discount_name = $("#discount_name").val();
        var start_time = $("#start_time").val();
        var end_time = $("#end_time").val();
        var obj = $(".goods-list input[type='number'][name='discount']");
        var range_type = 0;
        var goods_id_array = '';
        var goods_type_crowd = $(".goods_type_select_c .goods_type_item.selected").attr("data-goods-type");//商品活动对象

        obj.each(function(i){
            var dis = obj.eq(i).val() ? obj.eq(i).val() : 10;
            var gid = obj.eq(i).attr("goodsid");
            goods_id_array += ','+gid+':'+ dis;
            if(dis >10 || dis <= 0 ){
                showMessage('error', "折扣必须在0.01-10之间");
                flag = true;
                return false;
            }else{
                flag = false;
            }
        });
        goods_id_array = goods_id_array.substring(1);

        if(verify(discount_name, start_time, end_time, goods_id_array, range_type)){
            if(flag){
                return;
            }
            $.ajax({
                type : "post",
                url : "{:__URL('ADMIN_MAIN/promotion/adddiscount')}",
                data : {
                    'discount_name' : discount_name,
                    'start_time' : start_time,
                    'end_time' : end_time,
                    'goods_id_array' : $("#goods_id_array").val(),
                    'goods_type_crowd' : goods_type_crowd
                },
                success : function(data) {
                    if (data["code"] > 0) {
                        showMessage('success', data["message"],"{:__URL('ADMIN_MAIN/promotion/getdiscountlist')}");
                    }else{
                        showMessage('error', data["message"]);
                        flag = false;
                    }
                }
            });
        }
    }

    /**
     *模块输入信息验证
     */
    function verify(discount_name, start_time, end_time, goods_id_array){
        if(discount_name == ''){
            $("#discount_name").parent().find('.error').show();
            return false;
        }else{
            $(".error").hide();
        }
        if(start_time == '' || end_time == ''){
            $("#end_time").next('.error').show();
            return false;
        }else{
            $(".error").hide();
        }
        var dataTime = nowDate();
        var now = new Date(dataTime.replace("-", "/").replace("-", "/"));
        var end = new Date(end_time.replace("-", "/").replace("-", "/"));
        var start = new Date(start_time.replace("-", "/").replace("-", "/"));
        if(end < now){
            $("#end_time").next().next('.error').show();
            return false;
        }else{
            $(".error").hide();
        }
        if(end < start){
            $("#end_time").next().next().next('.error').show();
            return false;
        }else{
            $(".error").hide();
        }
        if(BatchSend.length == 0){
            $(".js-select-goods").find(".error").html('请至少选择一件商品').show();
            return false;
        }else{
            $(".error").hide();
        }
        return true;
    }

    function nowDate(){
        var myDate = new Date();
        //获取当前年
        var year=myDate.getFullYear();
        //获取当前月
        var month=myDate.getMonth()+1;
        //获取当前日
        var date=myDate.getDate();
        var h=myDate.getHours();//获取当前小时数(0-23)
        var m=myDate.getMinutes();//获取当前分钟数(0-59)
        var s=myDate.getSeconds();
        var now=year+'-'+month+"-"+date+" "+h+':'+m+":"+s;
        return now;
    }

    $(".goods_type_select_c .goods_type_item").click(function(){
        var value = $(this).children("input").val();

        if(value!=0){
            $('#inside_price_dl').css('display','block');
        }else{
            $('#inside_price_dl').css('display','none');
        }
        $(".goods_type_select_c .goods_type_item").removeClass("selected");
        $(this).addClass("selected");
        var goods_type = $(this).attr("data-goods-type");
        // 实物商品
        $('#txtProductCount').parents('dl').show();
        $("[data-c='block-goods-sku']").show().next().show();
        $(".js-virtual-goods-type-block").hide();
        $("#presell_set").show();
        $(".goods_type_select_c .goods_type_item input[name='virtual_goods_type']").prop("checked",false);
    })
</script>
{/block}